package com.demo.zyt.modules.app.service.impl;


import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.demo.zyt.common.dto.user.WxUserDTO;
import com.demo.zyt.common.exception.RRException;
import com.demo.zyt.common.utils.AppResultEnum;
import com.demo.zyt.config.CommonParameters;
import com.demo.zyt.config.ConfigConstant;
import com.demo.zyt.modules.app.dao.SysConfigDao;
import com.demo.zyt.modules.app.dao.WxInviteDao;
import com.demo.zyt.modules.app.dao.WxUserDao;
import com.demo.zyt.modules.app.dao.WxUserPointTrackDao;
import com.demo.zyt.modules.app.entity.WxInviteEntity;
import com.demo.zyt.modules.app.entity.WxUserEntity;
import com.demo.zyt.modules.app.entity.WxUserPointTrackEntity;
import com.demo.zyt.modules.app.service.WxUserService;


@Service("wxUserService")
public class WxUserServiceImpl extends ServiceImpl<WxUserDao, WxUserEntity> implements WxUserService {
	
	@Autowired
	private SysConfigDao sysConfigDao;
	
	@Autowired
	private WxUserPointTrackDao wxUserPointTrackDao;
	
	@Autowired
	private WxInviteDao wxInviteDao;

	@Override
	public void checkUserPhone(String phone, Integer userId) {
		
		WxUserEntity wxUserEntity = this.baseMapper.selectById(userId);
		
		if ( !StringUtils.isEmpty(wxUserEntity.getMobile()) ) {
			throw new RRException(AppResultEnum.USER_ALREADY_BIND_PHONE);
		}
		
		EntityWrapper<WxUserEntity> wrapper = new EntityWrapper<>();
		
		wrapper.eq("mobile", phone);
		
		if ( this.selectOne(wrapper) != null ) {
			throw new RRException(AppResultEnum.PHONE_ALREADY_BIND);
		}
	}

	@Override
	@Transactional
	public void bindPhone(Integer userId, WxUserDTO wxUserDTO) {
		
		WxUserEntity wxUserEntity = this.selectById(userId);
		
		wxUserEntity.setMobile(wxUserDTO.getMobile());
		
		Integer bindMoiblePoint = Integer.parseInt(this.sysConfigDao.selectById(ConfigConstant.BIND_MOBILE_POINT).getParamValue());
		
		wxUserEntity.setUserPoint( wxUserEntity.getUserPoint().intValue() + bindMoiblePoint.intValue());
		
		this.updateById(wxUserEntity);
		
		WxUserPointTrackEntity wxUserPointTrackEntity = new WxUserPointTrackEntity();
		
		wxUserPointTrackEntity.setCreatedDate(new Date());
		wxUserPointTrackEntity.setRemark("绑定手机");
		wxUserPointTrackEntity.setType(CommonParameters.POINT_TYPE.BIND);
		wxUserPointTrackEntity.setUserid(userId);
		wxUserPointTrackEntity.setPoint(bindMoiblePoint);
		
		this.wxUserPointTrackDao.insert(wxUserPointTrackEntity);
		
	}

	@Override
	@Transactional
	public void save(WxUserEntity wxUserEntity, WxInviteEntity wxInviteEntity) {
		
		Integer defaultPoint = Integer.parseInt(this.sysConfigDao.selectById(ConfigConstant.USER_DEFAULT_POINT).getParamValue());
		
		wxUserEntity.setUserPoint(defaultPoint);
		
		this.baseMapper.insert(wxUserEntity);
		
		WxUserPointTrackEntity wxUserPointTrackEntity = new WxUserPointTrackEntity();
		
		wxUserPointTrackEntity.setCreatedDate(new Date());
		wxUserPointTrackEntity.setRemark("首次登陆");
		wxUserPointTrackEntity.setType(CommonParameters.POINT_TYPE.REG);
		wxUserPointTrackEntity.setUserid(wxUserEntity.getUserid());
		wxUserPointTrackEntity.setPoint(defaultPoint);
		
		this.wxUserPointTrackDao.insert(wxUserPointTrackEntity);
		
		// 邀请人, 二维码邀请
		if ( wxInviteEntity.getInviteUserid() != null && wxInviteEntity.getQrcodeId() != null) {
			// 别人邀请过来的用户
			wxInviteEntity.setCreatedDate(new Date());
			wxInviteEntity.setUserid(wxUserEntity.getUserid());
			this.wxInviteDao.insert(wxInviteEntity);
		}
		
		// 普通分享, 新用户点进来
		if ( wxInviteEntity.getInviteUserid() != null && wxInviteEntity.getQrcodeId() == null) {
			// 更新对应积分
			WxUserEntity invitedUseEntity = this.baseMapper.selectById(wxInviteEntity.getInviteUserid());
			
			Integer invitedUserPoint = Integer.valueOf(this.sysConfigDao.selectById(ConfigConstant.SHARE_INVITE_USER).getParamValue());
			
			invitedUseEntity.setUserPoint(invitedUseEntity.getUserPoint().intValue() + invitedUserPoint.intValue());
			
			this.baseMapper.updateById(invitedUseEntity);
			
			WxUserPointTrackEntity invitedWxUserPointTrackEntity = new WxUserPointTrackEntity();
			
			invitedWxUserPointTrackEntity.setCreatedDate(new Date());
			invitedWxUserPointTrackEntity.setRemark("邀请新用户");
			invitedWxUserPointTrackEntity.setType(CommonParameters.POINT_TYPE.SHARE_INVITE_USER);
			invitedWxUserPointTrackEntity.setUserid(wxInviteEntity.getInviteUserid());
			invitedWxUserPointTrackEntity.setPoint(invitedUserPoint);
			
			this.wxUserPointTrackDao.insert(invitedWxUserPointTrackEntity);
		}
		
	}

	@Override
	public void updateWeChatUser(WxUserEntity wxUserEntity, Integer userId) {
		WxUserEntity entity = this.baseMapper.selectById(userId);
		
		entity.setCity(wxUserEntity.getCity());
		entity.setCountry(wxUserEntity.getCountry());
		entity.setHeadimgurl(wxUserEntity.getHeadimgurl());
		entity.setLanguage(wxUserEntity.getLanguage());
		entity.setNickname(wxUserEntity.getNickname());
		entity.setProvince(wxUserEntity.getProvince());
		entity.setSex(wxUserEntity.getSex());
		
		this.baseMapper.updateById(entity);
		
	}

	@Override
	public void updateUserVipStatusByOpenId(String openId) {
		WxUserEntity entity = new WxUserEntity();
		entity.setOpenid(openId);
		entity = this.baseMapper.selectOne(entity);
		entity.setIsVip(CommonParameters.IS_EFFECT.YES);
		this.baseMapper.updateById(entity);
		
	}

	@Override
	@Transactional
	public void deductUserPoint(Integer userId, Integer planQuery, String paramValue) {
		
		WxUserEntity wxUserEntity = this.selectById(userId);
		
		if ( wxUserEntity.getUserPoint().intValue() - Integer.parseInt(paramValue) < 0 ) {
			throw new RRException(AppResultEnum.USER_POINT_LACK);
		}
		
		wxUserEntity.setUserPoint(wxUserEntity.getUserPoint().intValue() - Integer.parseInt(paramValue));
		
		this.updateById(wxUserEntity);
		
		WxUserPointTrackEntity wxUserPointTrackEntity = new WxUserPointTrackEntity();
		
		wxUserPointTrackEntity.setCreatedDate(new Date());
		if ( planQuery.intValue() == CommonParameters.POINT_TYPE.PLAN_QUERY.intValue()) {
			wxUserPointTrackEntity.setRemark("招生计划查询");
		} else if ( planQuery.intValue() == CommonParameters.POINT_TYPE.PLAN_HIS_MAJOR.intValue()) {
			wxUserPointTrackEntity.setRemark("历年专业查询");
		} else {
			wxUserPointTrackEntity.setRemark("历年考生查询");
		}
		
		wxUserPointTrackEntity.setType(planQuery);
		wxUserPointTrackEntity.setUserid(userId);
		wxUserPointTrackEntity.setPoint(-Integer.parseInt(paramValue));
		
		this.wxUserPointTrackDao.insert(wxUserPointTrackEntity);
		
	}

	@Override
	public void addUserPoint(Integer userId) {
		
		Date date = new Date();

		Calendar startTime = Calendar.getInstance();

		startTime.setTime(date);
		startTime.set(Calendar.HOUR_OF_DAY, 0);
		startTime.set(Calendar.MINUTE, 0);
		startTime.set(Calendar.SECOND, 0);

		Calendar endTime = Calendar.getInstance();

		endTime.setTime(date);
		endTime.set(Calendar.HOUR_OF_DAY, 23);
		endTime.set(Calendar.MINUTE, 59);
		endTime.set(Calendar.SECOND, 59);
		
		EntityWrapper<WxUserPointTrackEntity> entityWrapper = new EntityWrapper<>();
		
		entityWrapper.gt("created_date", startTime.getTime());
		entityWrapper.lt("created_date", endTime.getTime());
		entityWrapper.eq("userid", userId);
		entityWrapper.eq("type", CommonParameters.POINT_TYPE.SUCCESS_SHARE);
		
		List<WxUserPointTrackEntity> trackList = this.wxUserPointTrackDao.selectList(entityWrapper);
		
		if ( trackList.size() >=  Integer.parseInt(this.sysConfigDao.selectById(ConfigConstant.SHARE_DAILY_TIME).getParamValue())) {
			throw new RRException(AppResultEnum.SHARE_TIME_ERROR);
		}
		
		// 更新对应积分
		WxUserEntity userEntity = this.baseMapper.selectById(userId);
		
		Integer addPoint = Integer.parseInt(this.sysConfigDao.selectById(ConfigConstant.SHARE_POINT).getParamValue());
		
		userEntity.setUserPoint(userEntity.getUserPoint().intValue() + addPoint.intValue());
		
		this.baseMapper.updateById(userEntity);
		
		WxUserPointTrackEntity invitedWxUserPointTrackEntity = new WxUserPointTrackEntity();
		
		invitedWxUserPointTrackEntity.setCreatedDate(new Date());
		invitedWxUserPointTrackEntity.setRemark("分享小程序");
		invitedWxUserPointTrackEntity.setType(CommonParameters.POINT_TYPE.SUCCESS_SHARE);
		invitedWxUserPointTrackEntity.setUserid(userId);
		invitedWxUserPointTrackEntity.setPoint(addPoint);
		
		this.wxUserPointTrackDao.insert(invitedWxUserPointTrackEntity);
		
	}
	
}
